import {createRouter, createWebHistory} from 'vue-router'
import Home from "@/views/home/Home";
import Layout from "@/layout/Layout";
import home from "@/views/home/Home";
import request from "@/utils/request";
import { isLoggedIn, getToken } from '@/utils/auth';


const routes = [
    {
        path: '/',
        name: 'Home',
        component: home
    },
    {
        path: '/market/jobs',
        name: 'JobRecommend',
        component: () => import('@/views/home/JobRecommend.vue')
    },
    {
        path: '/career-life',
        name: 'CareerLife',
        component: () => import('@/views/CareerLife.vue')
    },
    {
        path: '/layout',
        name: 'Layout',
        component: Layout,
        redirect: "/",
        children: [
            {
                path: '/',
                name: 'Home',
                component: () => import("@/views/home/Home")
            },
        ]
    },
    {
        path: '/login',
        name: 'Login',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import('@/views/login/Login')
    },
    {
        path: '/register',
        name: 'Register',
        component: () => import('@/views/login/Register')
    },
    {
        path: '/person',
        name: 'Person',
        component: () => import('@/views/Person')
    },
    {
        path: '/password',
        name: 'Password',
        component: () => import('@/views/Password')
    },
    {
        path: '/resume-analysis',
        name: 'ResumeAnalysis',
        component: () => import('@/views/ResumeAnalysis')
    },
    {
        path: '/career-planning',
        name: 'CareerPlanning',
        component: () => import('@/views/CareerPlanning')
    },
    {
        path: '/mock-interview',
        name: 'MockInterview',
        component: () => import('@/views/Interview')
    },
    {
        path: '/mock-interview/chat',
        name: 'MockInterviewChat',
        component: () => import('@/views/InterviewChat')
    },
    {
        path: '/ai-chat',
        name: 'AiChat',
        component: () => import('@/views/AiChat')
    },
    {
        path: '/interview-evaluation',
        name: 'InterviewEvaluation',
        component: () => import('@/views/InterviewEvaluation')
    },
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    console.log('=== 路由守卫开始 ===');
    console.log('从:', from.path, '到:', to.path);
    console.log('目标路由名称:', to.name);
    console.log('当前是否已登录:', isLoggedIn());
    
    // 如果访问登录页面或注册页面，直接放行
    if (to.path === '/login' || to.path === '/register') {
        console.log('访问登录或注册页面，直接放行');
        next()
        return
    }
    
    // 检查是否已登录
    if (isLoggedIn()) {
        console.log('用户已登录，允许访问');
        next()
    } else {
        console.log('用户未登录，重定向到登录页');
        next('/login')
    }
    
    console.log('=== 路由守卫结束 ===');
})

export default router
